<template>
    <view class="container">
        <view class="login-box">
            <!-- 标题 -->
            <text class="title">商家登录</text>

            <!-- 表单区域 -->
            <uni-forms ref="form">
                <!-- 账号输入 -->
                <uni-forms-item name="account">
                    <uni-easyinput v-model="formData.account" placeholder="请输入账号/手机号" prefix-icon="person" focus />
                </uni-forms-item>

                <!-- 密码输入 -->
                <uni-forms-item name="password">
                    <uni-easyinput v-model="formData.password" placeholder="请输入密码" prefix-icon="locked" password />
                </uni-forms-item>

                <!-- 记住密码和忘记密码 -->
                <view class="flex-between">
                    <label class="remember-password">
                        <checkbox :checked="rememberPassword" @click="toggleRemember" />
                        记住密码
                    </label>
                    <text class="forgot-password" @click="gotoForgot">忘记密码？</text>
                </view>

                <!-- 登录按钮 -->
                <button class="login-btn" type="primary" @click="handleLogin" :loading="loading">登录</button>

                <!-- 注册提示 -->
                <view class="register-tip">
                    没有账号？<text class="register-link" @click="gotoRegister">立即注册</text>
                </view>
            </uni-forms>
        </view>
    </view>
</template>

<script>
    import {
        setToken,
        getToken,
        setUserInfo
    } from "../tools/cookieMgr"
    import {
        checkLogin,
        login
    } from "@/pages/apis/login.js"
    import {
        isLogin
    } from '../tools/tools'
    export default {
        data() {
            return {
                formData: {
                    account: '',
                    password: ''
                },
                rememberPassword: false,
                loading: false
            }
        },
        onShow: async () => {
            const res = await checkLogin();
            if (res.code >= 0) {
                setUserInfo(res.data)
                uni.navigateTo({
                    url: '/pages/home/home'
                })
            }
        },
        mounted() {
            // 检查本地存储是否有记住的密码
            const savedAccount = uni.getStorageSync('savedAccount')
            const savedPassword = uni.getStorageSync('savedPassword')
            if (savedAccount && savedPassword) {
                this.formData = {
                    account: savedAccount,
                    password: savedPassword
                }
                this.rememberPassword = true
            }
        },
        methods: {
            // 登录处理
            async handleLogin() {
                if (!this.formData.account || !this.formData.password) {
                    uni.showToast({
                        title: '请输入账号和密码',
                        icon: 'none'
                    })
                    return
                }

                this.loading = true
                try {
                    // 这里替换为实际的登录接口调用
                    const res = await login(this.formData);
                    // 登录成功处理
                    if (res.code >= 0) {
                        // 记住密码处理
                        if (this.rememberPassword) {
                            uni.setStorageSync('savedAccount', this.formData.account)
                            uni.setStorageSync('savedPassword', this.formData.password)
                        } else {
                            uni.removeStorageSync('savedAccount')
                            uni.removeStorageSync('savedPassword')
                        }
                        setToken(res.data.token)
                        uni.reLaunch({
                            url: '/pages/home/home'
                        })
                    } else {
                        uni.showToast({
                            title: res.message || '登录失败',
                            icon: 'none'
                        })
                    }
                } catch (error) {
                    uni.showToast({
                        title: '请求失败，请重试' + error,
                        icon: 'none'
                    })
                }
                this.loading = false
            },

            // 切换记住密码
            toggleRemember() {
                this.rememberPassword = !this.rememberPassword
            },

            // 跳转忘记密码
            gotoForgot() {
                uni.navigateTo({
                    url: '/pages/auth/forgot'
                })
            },

            // 跳转注册
            gotoRegister() {
                uni.navigateTo({
                    url: '/pages/auth/register'
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    .container {
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #f8f8f8;

        .login-box {
            width: 90%;
            padding: 40rpx;
            background-color: #fff;
            border-radius: 20rpx;
            box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);

            .title {
                display: block;
                text-align: center;
                font-size: 40rpx;
                font-weight: bold;
                color: #333;
                margin-bottom: 60rpx;
            }

            .flex-between {
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin: 30rpx 0;

                .remember-password {
                    font-size: 28rpx;
                    color: #666;
                    display: flex;
                    align-items: center;
                }

                .forgot-password {
                    font-size: 28rpx;
                    color: #007AFF;
                }
            }

            .login-btn {
                margin-top: 40rpx;
                height: 90rpx;
                line-height: 90rpx;
                border-radius: 45rpx;
                font-size: 34rpx;
            }

            .register-tip {
                text-align: center;
                margin-top: 40rpx;
                color: #666;
                font-size: 28rpx;

                .register-link {
                    color: #007AFF;
                    margin-left: 10rpx;
                }
            }
        }
    }
</style>